/*
 * Hyuga: A Density-Grid Stream Clustering Platform.
 * Copyright (C) 2014 PUC-Rio/Laboratory for Advanced Collaboration
 *
 * Hyuga is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Hyuga is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hyuga.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.pucrio.inf.lac.konoha.hyuga.events.cell;

import java.util.ArrayList;
import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import br.pucrio.inf.lac.konoha.hyuga.events.CEPEvent;

/**
 * Represent a context partition (cell) clustering event. The event is composed by a CellCoreEvent (the cluster core) 
 * and its neighbors (a CellContentEvent array). This event is emitted when a context partition (cell) detects a
 * cluster, i.e., when the number of mobile nodes mapped to the context partition is over minPts.
 *
 * @author Marcos Roriz <mroriz@inf.puc-rio.br>
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class CellClusterEvent extends CEPEvent {
    // Cell Core
    protected CellCoreEvent core;
        
    // Neighbor cells
    private CellContentEvent[] neighbors;
    
    // Distributed Flag
    private boolean distributed;
        
    // Empty Constructor
    public CellClusterEvent() {
        this(null, null);
    }
    
    // Basic Constructor
    public CellClusterEvent(CellCoreEvent core) {
        this(core, null);
    }
    
    // Basic Constructor
    public CellClusterEvent(CellCoreEvent core, boolean distributed) {
        this(core, null, distributed);
    }

    // Default Constructor
    public CellClusterEvent(CellCoreEvent core, CellContentEvent[] neighbors) {
        this(core, neighbors, false);
    }
    
    // Full Constructor
    public CellClusterEvent(CellCoreEvent core, CellContentEvent[] neighbors, boolean distributed) {
        this.core      = core;
        this.neighbors = neighbors;

        // Type
        this.type = TYPE.CELLCLUSTER;
        this.distributed = distributed;
    }
    
    /**
     * Retrieve the number of mobile nodes in the cell cluter, that is, the sum of the mobile nodes mapped to the core
     * cell and its neighbor cell.
     * 
     * @return the number of mobile nodes in the cell cluster.
     */
    public int size() {
        int size = 0;
        if (core != null) size += core.size();
        
        if (neighbors != null) {
            for (CellContentEvent neighbor : neighbors) {
                size += neighbor.size();
            }
        }
        
        return size;
    }
    
    @Override
    public String toString() {
        return "(" + core.getX() + ", " + core.getY() + ") ~ " + size();
    }

    //////////////////////////////////////////////////////////////////////////
    // Gets and Sets
    //////////////////////////////////////////////////////////////////////////
    
    public CellContentEvent[] toCellContentArray() {
        ArrayList<CellContentEvent> clusterContent = new ArrayList<>();
        if (neighbors != null) {
            clusterContent.addAll(Arrays.asList(neighbors));
        }
        
        clusterContent.add(new CellContentEvent(core.getX(), core.getY(), core.getNodes()));
        
        return clusterContent.toArray(new CellContentEvent[clusterContent.size()]);
    }
    
    public CellCoreEvent getCore() {
        return core;
    }
    
    public CellContentEvent[] getNeighbors() {
        return neighbors;
    }

    public void setCore(CellCoreEvent core) {
        this.core = core;
    }
    
    public void setNeighbors(CellContentEvent[] neighbors) {
        this.neighbors = neighbors;
    }
    
    public boolean isDistributed() {
        return distributed;
    }

    public void setDistributed(boolean distributed) {
        this.distributed = distributed;
    }
}
